package com.camera2.activity;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.camera2.adapter.ExpertAdapter;
import com.camera2.adapter.SetListViewHeight;
import com.camera2.bean.ApplicationHelper;
import com.camera2.bean.ExpertBean;
import com.camera2.http.GetExpert;

import java.util.List;


public class ExpertList extends Activity implements
        SwipeRefreshLayout.OnRefreshListener {
    private static final String TAG = "ExpertList";
    private ListView lv;
    private SwipeRefreshLayout mSwipeLayout;
    private ExpertAdapter expertAdapter;
    private String id;
    private List<ExpertBean> list;

    private int page;
    private Button bt_warn_next;
    private Button bt_warn_up;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String info = (String) msg.obj;
                    if (info.equals("success")) {
                        initEvent();
                    } else {
                        Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "连接网络失败,请检查网络是否连接", Toast.LENGTH_SHORT)
                            .show();
                    finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expert_list);
        bt_warn_up = findViewById(R.id.bt_warn_up);
        bt_warn_next = findViewById(R.id.bt_warn_next);
        bt_warn_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "正在加载，请稍候", Toast.LENGTH_SHORT).show();
                if (page >= 1) {
                    page = page - 1;
                    getWarnPage();
                }
            }
        });
        bt_warn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "正在加载，请稍候", Toast.LENGTH_SHORT).show();
                page = page + 1;
                getWarnPage();
            }
        });
        bt_warn_up.setVisibility(View.GONE);
        bt_warn_next.setVisibility(View.GONE);
        page = 1;
        Toast.makeText(getApplicationContext(), "获取数据中，请稍后", Toast.LENGTH_SHORT).show();
        init();

    }


    public void init() {

        lv = findViewById(R.id.lv_expert_list);
        mSwipeLayout = findViewById(R.id.swipe_warn_list);
        mSwipeLayout.setOnRefreshListener(ExpertList.this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        getWarnPage();

    }

    public void initEvent() {
        mSwipeLayout.setRefreshing(false);
        list = ((ApplicationHelper) getApplication().getApplicationContext()).getExpertList();

        if (list != null) {
            if (list.size() > 5) {
                bt_warn_up.setVisibility(View.VISIBLE);
                bt_warn_next.setVisibility(View.VISIBLE);
            }
            expertAdapter = new ExpertAdapter(this, list);
            lv.setAdapter(expertAdapter);
            SetListViewHeight.setListViewHeightBasedOnChildren(lv);
//            Log.i(TAG, "initEvent: " + list);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id1) {
                    switch (position) {
                        default: {
                            ((ApplicationHelper) getApplication().getApplicationContext()).setExpertBean(list.get(position));
                            Intent intent = new Intent(getApplicationContext(), ExpertDetail.class);
                            startActivity(intent);
                            break;
                        }
                    }
                }
            });
        } else {
            Toast.makeText(getApplicationContext(), "没有留言信息", Toast.LENGTH_SHORT).show();
        }

    }


    public void onRefresh() {
        getWarnPage();
        mSwipeLayout.setRefreshing(true);

    }


    public void getWarnPage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String log = GetExpert.getExpertList(getApplicationContext());
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = log;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 0;
                    handler.sendMessage(msg);
                }

            }
        }).start();
    }
}
